package window.utils;

import window.MainTab;
import window.childTab.payloadTab;
import window.childTab.targetTab;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 自定义组件
 */
public class customObject {
    /**
     * Tab关闭按钮，待优化
     */
    public static JButton createCloseButton(){
        JButton button = new JButton();
        button.setText("x");
        button.setToolTipText("Close the tab");
        button.setBorderPainted(false);
        button.setPreferredSize(new Dimension(12,12));
        button.setBorder(BorderFactory.createRaisedBevelBorder());

        return button;
    }

    /**
     * 创建一个新的序号面板：1、2、3...
     */
    public static JTabbedPane createNumPanel(String request) {
        // target+payload Panel
        JTabbedPane tarPayPanel = new JTabbedPane();

        // targetTabPanel
        JPanel targetTabPanel;

        // payloadTabPanel
        JPanel payloadTabPanel;

        // 对targetTab设置布局
        targetTab targetTab = new targetTab();
        targetTab.setLayout(request);
        targetTabPanel = targetTab.getjPanel();

        // 对payloadTab设置布局
        payloadTab payloadTab = new payloadTab();
        payloadTab.setLayout();
        payloadTabPanel = payloadTab.getjPanel();

        // 对target+payload设置布局
        tarPayPanel.addTab("Target", targetTabPanel);
        tarPayPanel.addTab("Payloads", payloadTabPanel);

        return tarPayPanel;
    }


    /**
     * 创建一个新的序号标签（主要包含关闭按钮），待优化
     * @return JPanel
     */
    public static JPanel createNumTab(int number) {
        JPanel tabPanel = new JPanel();
        JLabel tabLabel = new JLabel(String.valueOf(number));
        JButton closeButton = customObject.createCloseButton();

        // 关闭按钮事件监听
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainTab.numPanel.remove(number-1);
                MainTab.number--;
            }
        });

        tabPanel.add(tabLabel);
        tabPanel.add(closeButton);
        tabPanel.setOpaque(false);

        return tabPanel;
    }
}
